package user

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
	"sort"
	"time"
)

type PaihangbangBody struct {
	Name    string  `json:"name"`
	Image   string  `json:"image"`
	Price   float64 `json:"price"`
	Paihang int64   `json:"paihang"`
	Uid     int64   `json:"uid"`
}

func Paihangbang(ctx *gin.Context) {
	request := ShangjuansBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid

	//startOfToday := time.Now().Truncate(24 * time.Hour)
	startTimestamp := getTodayMidnightTimestamp()
	//fmt.Println(startTimestamp)
	var liushui []models.Userdayprice
	data_source.Db.Where("addtime = ?", startTimestamp).Find(&liushui)
	if len(liushui) == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "今天还没有排名",
		})
		return
	}
	var useridarr []int64
	for _, v := range liushui {
		useridarr = append(useridarr, v.Uid)
	}
	var users []models.User
	data_source.Db.Where("id in (?)", useridarr).Find(&users)
	var paihangs []PaihangbangBody
	for _, v := range users {
		var price float64
		for _, l := range liushui {
			if v.ID == l.Uid {
				price = price + l.Price
			}
		}
		paihang := PaihangbangBody{
			Name:  v.Nickname,
			Image: v.Logo,
			Price: price,
			Uid:   v.ID,
		}
		paihangs = append(paihangs, paihang)
	}
	sort.Slice(paihangs, func(i, j int) bool {
		return paihangs[i].Price > paihangs[j].Price
	})

	var paihanglist []PaihangbangBody
	var paihanguser PaihangbangBody
	var i int64 = 1
	for _, p := range paihangs {
		p.Paihang = i
		if i <= 10 {
			paihanglist = append(paihanglist, p)
		}
		i += 1
		if p.Uid == uid {
			paihanguser = p
		}
	}

	data := PaihangbangRBody{
		Paihang:     paihanglist,
		Paihanguser: paihanguser,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type PaihangbangRBody struct {
	Paihang     []PaihangbangBody `json:"paihang"`
	Paihanguser PaihangbangBody   `json:"paihanguser"`
}

func Paihangbangjiangli(ctx *gin.Context) {

	//startOfToday := time.Now().Truncate(24 * time.Hour)
	startTimestamp := getTodayMidnightTimestamp()
	var liushui []models.Userdayprice
	data_source.Db.Where("addtime = ?", startTimestamp).Find(&liushui)
	if len(liushui) == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "今天还没有排名",
		})
		return
	}
	var useridarr []int64
	for _, v := range liushui {
		useridarr = append(useridarr, v.Uid)
	}
	var users []models.User
	data_source.Db.Where("id in (?)", useridarr).Find(&users)
	var paihangs []PaihangbangBody
	for _, v := range users {
		var price float64
		for _, l := range liushui {
			if v.ID == l.Uid {
				price = price + l.Price
			}
		}
		paihang := PaihangbangBody{
			Name:  v.Nickname,
			Image: v.Logo,
			Price: price,
			Uid:   v.ID,
		}
		paihangs = append(paihangs, paihang)
	}
	sort.Slice(paihangs, func(i, j int) bool {
		return paihangs[i].Price > paihangs[j].Price
	})

	var paihanglist []PaihangbangBody
	var i int64 = 1
	for _, p := range paihangs {
		p.Paihang = i
		if i <= 10 {
			paihanglist = append(paihanglist, p)
		}
		i += 1
	}

	var paiming []models.Paiming
	data_source.Db.Find(&paiming)
	now := time.Now()
	zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
	//fmt.Println(paihanglist)
	var pmjls []models.Paihangjl
	for _, ph := range paihanglist {
		for _, pm := range paiming {
			if ph.Paihang == pm.Paiming {
				pmjl := models.Paihangjl{
					Daytime: zeroToday,
					Uid:     ph.Uid,
					Paiming: pm.Paiming,
					Daibi:   pm.Daibi,
				}
				pmjls = append(pmjls, pmjl)

				var user models.User
				data_source.Db.Where("id = ?", ph.Uid).First(&user)
				userupdata := models.User{
					Daibi: user.Daibi + pm.Daibi,
				}
				data_source.Db.Where("id = ?", ph.Uid).Updates(&userupdata)

				userdaibi := models.Userdaibi{
					Uid:     ph.Uid,
					Status:  1,
					Addtime: time.Now().Unix(),
					Daibi:   pm.Daibi,
					Title:   "排行榜奖励",
					State:   1,
					Sydaibi: user.Daibi + pm.Daibi,
				}
				data_source.Db.Create(&userdaibi)

			}
		}
	}
	data_source.Db.Debug().Create(&pmjls)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func getTodayMidnightTimestamp() int64 {
	// 获取当前时间
	now := time.Now()

	// 构造今天零点的时间对象
	midnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())

	// 转换为时间戳（以秒为单位）
	timestamp := midnight.Unix()

	return timestamp
}
